﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ZoLone.Logics.Sys;
using ZoLone.Models.Sys;

namespace ZoLone.Areas.Admin.Controllers.Sys
{
    public class AdminController : BaseController
    {
        AdminLogic _logic = new AdminLogic();

        // GET: Admin/Admin
        public IActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public IActionResult GetData()
        {
            var data = _logic.GetAllAdmins();
            return Success(data);
        }
        public IActionResult Save(Sys_Admin info, int[] roles)
        {
            var re = _logic.SaveInfo(info, roles);
            if (re)
            {
                return Success();
            }
            return Error("Failed");
        }

        [HttpPost]
        public IActionResult GetAdminInfo(int adminId = 0)
        {
            AdminRoleLogic _roleLogic = new AdminRoleLogic();
            //get admin info
            var admin = _logic.GetAdminInfoById(adminId);
            //get admin role
            var hasRoles = _roleLogic.GetRolesByAdminId(adminId);
            string role_str = string.Empty;
            if (hasRoles != null && hasRoles.Count() > 0)
            {
                role_str = string.Join(",", hasRoles.Select(c => c.Id));
            }
            return Success(new { info = admin, roles = role_str });
        }

        [HttpPost]
        public IActionResult GetALLRoles()
        {
            RoleLogic _roleLogicAll = new RoleLogic();
            var allRoles = _roleLogicAll.GetAllRoles();
            return Success(allRoles.Select((p, i) => new { id = p.Id, text = p.Name }));
        }

        [HttpPost]
        public IActionResult Delete(int adminId)
        {
            var result = _logic.Delete(adminId);
            if (result)
            {
                return Success();
            }
            return Error();
        }
        [HttpPost]
        public IActionResult ResetPassword(string password, string confirmPassword, int adminId)
        {
            var result = _logic.resetPassword(password, confirmPassword, adminId);
            if (result)
            {
                return Success();
            }
            return Error();
        }
    }
}